package com.study.animationstudy.ui

import android.graphics.ImageDecoder
import android.graphics.drawable.Animatable
import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.study.animationstudy.R
import com.study.animationstudy.base.BaseActivity
import com.study.animationstudy.databinding.ActivityGifBinding
import com.study.animationstudy.util.GifImage

class GifActivity : BaseActivity<ActivityGifBinding>() {

    val typeArray = arrayOf("显示GIF动图","显示WebP动图")

    override fun ActivityGifBinding.initBinding() {
//        showGifAnim()
        initTypeSpinner()
    }

    fun initTypeSpinner(){
        val typeAdapter = ArrayAdapter<String>(this,R.layout.item_select,typeArray)
        mBinding.spType.apply {
            prompt = "请选择图像类型"
            adapter = typeAdapter
            onItemSelectedListener = object :OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    if (position == 0){
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                            // 利用Android9新增的AnimatedImageDrawable显示GIF动画
                            showAnimateDrawable(R.drawable.happy)
                        } else {
                            showGifAnim() // 借助帧动画播放gif动图
                        }
                    } else if (position == 1){
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                            showAnimateDrawable(R.drawable.world_cup_2014)
                        } else {
                            Toast.makeText(
                                this@GifActivity,
                                "播放WebP动图需要Android9及更高版本",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {

                }

            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.P)
    fun showAnimateDrawable(imgId:Int){
        try {
            //利用Android9新增的ImageDecoder获取图像来源
            val source = ImageDecoder.createSource(resources,imgId)
            // 从数据源中解码得到图形数据
            val drawable = ImageDecoder.decodeDrawable(source)
            mBinding.ivGif.setImageDrawable(drawable)
            if (drawable is Animatable){
                (mBinding.ivGif.drawable as Animatable).start()
            }
        } catch (e:java.lang.Exception){
            e.printStackTrace()
        }
    }

    /**
     * 借助于帧动画技术显示gif
     * 将GIF文件分解为一系列图片数据，并获取每帧的持续时间，然后通过动画图形动态加载每帧图片。
     */
    fun showGifAnim(){
        val inputStream = resources.openRawResource(R.raw.welcome)
        val gifImage = GifImage()
        when(val code = gifImage.read(inputStream)){
            GifImage.STATUS_OK -> {
                //
                val frameList = gifImage.frames
                val animationDrawable = AnimationDrawable()
                frameList.forEach {
                    //BitmapDrawable用于把Bitmap格式转换为Drawable格式
                    val bitmapDrawable = BitmapDrawable(resources,it.image)
                    animationDrawable.addFrame(bitmapDrawable,it.delay)
                }
                //循环播放
                animationDrawable.isOneShot = false
                mBinding.ivGif.setImageDrawable(animationDrawable)
                animationDrawable.start()
            }
            GifImage.STATUS_FORMAT_ERROR -> {
                Toast.makeText(this, "该图片不是gif格式", Toast.LENGTH_LONG).show()
            }
            else -> {
                Toast.makeText(this, "gif图片读取失败:$code", Toast.LENGTH_LONG).show()
            }
        }
    }

}